<?php
$options = $this->my_options();

//main values
$urlserviceenable = $options['urlserviceenable'];
$urlservice = $options['urlservice'];
$urlautogen = $options['urlautogen'];
$urlbetaservices = $options['urlbetaservices'];

//Service Config
$apiuser_supr = $options['apiuser_supr'];
$apikey_supr = $options['apikey_supr'];
$apiuser_bitly = $options['apiuser_bitly'];
$apikey_bitly = $options['apikey_bitly'];
$apiuser_trim = $options['apiuser_trim'];
$apikey_trim = $options['apikey_trim'];
$apiuser_snip = $options['apiuser_snip'];
$apikey_snip = $options['apikey_snip'];
$snipprefix = $options['snipprefix'];
$apikey_cligs = $options['apikey_cligs'];
$apiuser_shortie = $options['apiuser_shortie'];
$apikey_shortie = $options['apikey_shortie'];
$apikey_pingfm = $options['apikey_pingfm'];
$apikey_awesm = $options['apikey_awesm'];

//tweet
$tweet = $options['tweet'];
$tweeted = get_post_meta($postid, 'tweet', true);

//shorturl
$shorturl = get_post_meta($postid, 'shorturl', true);
if(empty($shorturl)){$shorturl = get_post_meta($postid, 'short_url', true);}
$new_url = '';
if(empty($shorturl)){
	switch ($post_type){
		case 'post' : $posturl = get_option('home')."/index.php?p=".$postid; break;
		case 'page' : $posturl = get_option('home')."/index.php?page_id=".$postid; break;
		default : $posturl = get_permalink($postid); break;
	}
	
	//Main Service
	$this->url = $posturl;
	$selectedservice = $urlservice;
	
	switch ($urlservice){
		case 'supr': $this->name = htmlentities($apiuser_supr, ENT_QUOTES); $this->apikey = htmlentities($apikey_supr, ENT_QUOTES); break;
		case 'bitly': $this->name = htmlentities($apiuser_bitly, ENT_QUOTES); $this->apikey = htmlentities($apikey_bitly, ENT_QUOTES); break;
		case 'trim': $this->name = htmlentities($apiuser_trim, ENT_QUOTES); $this->apikey = htmlentities($apikey_trim, ENT_QUOTES); break;
		case 'snipurl': $this->name = htmlentities($apiuser_snip, ENT_QUOTES); $this->apikey = htmlentities($apikey_snip, ENT_QUOTES); $selectedservice = $snipprefix; break;		
		case 'cligs': $this->apikey = htmlentities($apikey_cligs, ENT_QUOTES); break;		
		case 'shortie': $this->name = htmlentities($apiuser_shortie, ENT_QUOTES); $this->apikey = htmlentities($apikey_shortie, ENT_QUOTES); break;		
		case 'pingfm': $this->apikey = htmlentities($apikey_pingfm, ENT_QUOTES); $this->pingfmapi = 'f51e33510d3cbe2ff1e16a4a4897f099'; break;
		case 'awesm': $this->apikey =  htmlentities($apikey_awesm, ENT_QUOTES); break;	
		default: break;
	}
	$this->service = $selectedservice;
	$new_url = $this->shorturl();
	
	//Beta Services
	if (empty($new_url)){
		if (class_exists('FTShortenExtended') && $urlbetaservices == 'yes'){
			$FTSE = new FTShortenExtended();
			$FTSE->name = $this->name;
			$FTSE->service = $this->service;
			$FTSE->url = $this->url;
			$FTSE->apikey = $this->apikey;
			$new_url = $FTSE->shorturl();
		}
	}
	
	//Update and Tweet	
	if(!empty($new_url)){
		update_post_meta($postid, 'shorturl', $new_url);
		if($to_tweet){if($tweet == 'auto' && empty($tweeted)){ $this->tweet_logic($postid, $new_url); }}
	}
//end !shorturl
}
if ($to_return){if(empty($new_url)){return $new_url;}else{return $shorturl;}}
?>